package com.yan.interview.registry.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.yan.interview.registry.schema.InterviewRegistry;
import com.yan.interview.registry.service.facade.InterviewRegistryDaoService;
import com.yan.interview.registry.vo.RequestVo;
import com.yan.interview.registry.vo.ResponseVo;

@RestController
public class InterviewRegistryRestController {

	@Autowired
	private InterviewRegistryDaoService interviewRegistryDaoService;
	
	@RequestMapping(value = "/api/registries/datagrid", method = RequestMethod.POST,consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
	public ResponseVo queryWeights(RequestVo requestVo) throws JsonProcessingException {
		ResponseVo responseVo = new ResponseVo();
		responseVo.setSuccess(false);
		responseVo.setErrorMsg(null);

		Map<String, Object> condition = new HashMap<String, Object>();
				
		int pageNo = 1;
		if(requestVo != null && requestVo.getPage() != null && requestVo.getPage() >= 1){
			pageNo = requestVo.getPage();
		}
		
		int pageSize = 10;
		if(requestVo != null && requestVo.getRows() != null && requestVo.getRows() >= 0){
			pageSize = requestVo.getRows();
		}
		
		int offset = (pageNo-1)*pageSize;
		
		//第一个参数指定第一个返回记录行的偏移量，第二个参数指定返回记录行的最大数目。初始记录行的偏移量是 0(而不是 1)
		condition.put("offset", offset);
		condition.put("rows", pageSize);
		
		Long total = interviewRegistryDaoService.countInterviewRegistryByCondition(condition);
		List<InterviewRegistry> registries = interviewRegistryDaoService.findInterviewRegistryByCondition(condition);
		
		responseVo.setSuccess(true);
		responseVo.setErrorMsg(null);
		
		responseVo.setTotal(total);
		responseVo.setRows(registries);
		
		return responseVo;
	}
}
